//
//  YTFEmotionManager.swift
//  sinaWeibo
//
//  Created by pro on 16/3/9.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit

//这个类用于管理表情
class YTFEmotionManager: NSObject {
    
    //定义四个数组接收表情模型
    //最近表情
    static var rescentEmotions: [YTFEmotion] = [YTFEmotion]()
    //默认表情
    static var defaultEmotions: [YTFEmotion] = YTFEmotionManager.readEmotions("com.sina.default")
    //emoji表情
    static var emojiEmotions: [YTFEmotion] = YTFEmotionManager.readEmotions("com.apple.emoji")
    //浪小花表情
    static var lxhEmotions: [YTFEmotion] = YTFEmotionManager.readEmotions("com.sina.lxh")
    
    //对外提供一个接口，返回一个元素是表情数组的数组
    class func getAllEmotions() -> [[[YTFEmotion]]] {
        
        return [pageEmotions(rescentEmotions),pageEmotions(defaultEmotions),pageEmotions(emojiEmotions),pageEmotions(lxhEmotions)]
        
    }
    
    //MARK: -- 最近表情添加
    class func addRecentEmotions(emotion: YTFEmotion?){
        
        if let emo = emotion {
            
            //判断最近表情数组中是否有这个表情，有就不添加，没有就添加
            if !rescentEmotions.contains(emo){
                rescentEmotions.insert(emo, atIndex: 0)
                
                //对数据中的数据个数进行判断，大于20个的就删除最后的那个
                if rescentEmotions.count > 20{
                rescentEmotions.removeLast()
                }
            }
            
        }
        
    }
    
    //获取表情数据,需要传入它的文件夹名
    class func readEmotions(package: String) -> [YTFEmotion] {
        
        //定义一个数组来接收表情
        var result = [YTFEmotion]()
        
        //1.获取emotions.bundle的路劲
        let bundlePath = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        
        //2.拼接文件夹路径
        let packagePath = (bundlePath! as NSString).stringByAppendingPathComponent(package)
        
        //3.获取plist路径
        let infoPath = (packagePath as NSString).stringByAppendingPathComponent("info.plist")
        
        //获取info字典
        let dict = NSDictionary(contentsOfFile: infoPath)!
        
        //获取里面的表情数组
        let emotions = dict["emoticons"] as! [[String:NSObject]]
        
        //遍历数组，给指定表情数组添加数据
        for emotion in emotions {
            
//            //字典转模型并添加
//            result.append(YTFEmotion(dict: emotion))
            let emo = YTFEmotion(dict: emotion)
            
            //在这里吧文件夹路径传给模型
            emo.path = packagePath
            
            result.append(emo)
            
        }
        
        return result
    }
    
    //对数组进行分页,20个一页
    class func pageEmotions(emotions:[YTFEmotion]) -> [[YTFEmotion]]{
        
        //定义一个数组来接收
        var result = [[YTFEmotion]]()
        
        //获取表情数
        let count = emotions.count
        
        //获取页数
        let page = (count - 1) / 20 + 1
        
        //切割原有数组
        for i in 0..<page{
            //定义一个切割长度
            var length = 20
            
            if i == page - 1{
                length = count - i*20
            }
            let subArray = (emotions as NSArray).subarrayWithRange(NSMakeRange(i*20, length)) as! [YTFEmotion]
            
            result.append(subArray)
        }
        
        
        
        return result
    }
    
    
}






